var orderId="";

//相关业务逻辑
$(function(){
   page.bind();
   page.getSelectValue("providerName","companyMaterial/getProviderName",null,0);//获取供应商
   page.getSelectValue("approvalUser","user/getDepartmentUser",{"deptName":"仓库"},1);
});

var table;
var page={//页面总变量
		bind:function(){
			//选择类型之后
			 $("select[name=type]").bind("change",function(){
				 var sendData=new Object();
				 sendData.providerName=$("select[name=providerName]").val();
				 sendData.type=$(this).val();
				 page.getSelectValue("purchasePeople","companyMaterial/getPurchasePeople",sendData,1);
			 });
			 
			 
			 //选择类型之后
			 $("select[name=purchasePeople]").bind("change",function(){
				 var sendData=new Object();
				 sendData.providerName=$("select[name=providerName]").val();
				 sendData.type=$("select[name=type]").val();
				 sendData.purchasePeople=$(this).val();
				 page.getSelectValue("id","companyMaterial/getPurchaseId",sendData,0);
			 });
			 
			 //选择类型之后
			 $("select[name=id]").bind("change",function(){
				 var purchaseId=$(this).val();
				 if(purchaseId==""){
					 return false;
				 }
				 var type = $("select[name=type]").val(); 
				 page.getPurchaseMaterialDate(purchaseId,type);
			 });
			 
			 //输入入库数
			 $('#inWarehuseTable tbody').on('keyup', 'input', function () {
				    var re=/^[0-9]+[0-9]*]*$/;
				    var count=$(this).val();
					if(!re.test(count)){
						count=count.substring(0,count.length-1);
					    $(this).val(count);
					 }
				});
			 
			 $("#savdInWarehouseBtn").bind("click",function(){
				 var purchaseId=$("select[name=id]").val();
				 var object=new Object();
				 var type = $("select[name=type]").val();
				 object.type=type;
				 object.purchasePeople=$("select[name=purchasePeople]").val();
				 object.approvalUser=$("select[name=approvalUser]").val();
				 object.id=purchaseId;
				 if(object.purchasePeople==""){
					 layAlert("请选择采购人");
					 return false;
				 }
				 if(object.id==""){
					 layAlert("请选择单号");
					 return false;
				 }
				 if(object.approvalUser==""){
					 layAlert("请选择验人");
					 return false;
				 }
				 var metailArray=new Array();
				 $("#inWarehuseTable tbody tr").each(function(){
					 var obj=new Object();
					 obj.purchaseDetailId = $(this).find("td .purchaseDetailId").val();
					 obj.materialCode=$(this).find("td .materialCode").val();
					 obj.count=$(this).children("td").eq(7).children("input").val();
					 obj.orderId = $(this).children("td").eq(6).text();
					 metailArray.push(obj);
				 });
				
				 if(metailArray.length<=0){
					 return false;
				 }
				 object.metailArray=JSON.stringify(metailArray);
				 ajaxUtil.ajaxRuquest("companyMaterial/savdInWarehouse",object,function(data){
					 layer.msg(data,{icon:1});
					 page.getPurchaseMaterialDate(purchaseId,type);
				 });
				 
			 });
		},
		//获取供应商
		getSelectValue:function(name,url,sendData,type){
			ajaxUtil.ajaxRuquest(url,sendData,function(data){
				var html="<option value=''>请选择</option>";
				for(var i=0;i<data.length;i++){
					if(type==0){
						html+="<option value="+data[i]+">"+data[i]+"</option>";
					}
					if(type==1){
						html+="<option value="+data[i].id+">"+data[i].userName+"</option>";
					}
				}
				$("select[name="+name+"]").html("");
				$("select[name="+name+"]").append(html);
			});
		},
		getPurchaseMaterialDate:function(purchaseId,purType){
			table=ajaxUtil.dataTablesAjax("inWarehuseTable",10,"companyMaterial/getPurchaseMaterialDate",
					{"id":purchaseId},[
					{"data":"factoryNo",render:function(data,type,row){
						return "<td><input  class='purchaseDetailId' type ='hidden' value="+row.id+">"+data+"</td>";
					}},  
					{"data":"materialName",render:function(data,type,row){
						return "<td><input class='materialCode' type ='hidden' value="+row.materialCode+">"+data+"</td>";
					}},
					{"data":"color"},
					{"data":"colorDescription"},
					{"data":"model"},
					{"data":"unit"},
					{"data":"orderId"},
					{"data":"count",render: function (data,type,row) {
						if(purType=="1"){
							data = data;
						}else{ 
							data = row.countb;
						}
						return "<input type='text' value='"+data+"' class='input-text' />"
					}}
			]);
		}
		
}
